
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>错误包 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="method.html" />
    
    
    <link rel="prev" href="destination.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    概览
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" >
            
                <span>
            
                    
                    快速入门
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../guides/project.html">
            
                <a href="../guides/project.html">
            
                    
                    项目结构和初始化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../guides/api.html">
            
                <a href="../guides/api.html">
            
                    
                    API
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../guides/doc.html">
            
                <a href="../guides/doc.html">
            
                    
                    生成 API 文档
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../guides/client.html">
            
                <a href="../guides/client.html">
            
                    
                    生成客户端
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" >
            
                <span>
            
                    
                    概念
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="cp.html">
            
                <a href="cp.html">
            
                    
                    Consumer && Producer
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="operator.html">
            
                <a href="operator.html">
            
                    
                    Operator
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="filter.html">
            
                <a href="filter.html">
            
                    
                    Filter
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="prefab.html">
            
                <a href="prefab.html">
            
                    
                    Prefab
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="modifier.html">
            
                <a href="modifier.html">
            
                    
                    Modifier
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="middleware.html">
            
                <a href="middleware.html">
            
                    
                    Middleware
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="context.html">
            
                <a href="context.html">
            
                    
                    Context
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.8" data-path="source.html">
            
                <a href="source.html">
            
                    
                    Parameter Generator
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.9" data-path="destination.html">
            
                <a href="destination.html">
            
                    
                    Destination Handler
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.3.10" data-path="error.html">
            
                <a href="error.html">
            
                    
                    错误包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.11" data-path="method.html">
            
                <a href="method.html">
            
                    
                    方法包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.12" data-path="configurer.html">
            
                <a href="configurer.html">
            
                    
                    配置器机制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.13" data-path="plugin.html">
            
                <a href="plugin.html">
            
                    
                    插件机制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.14" data-path="clients.html">
            
                <a href="clients.html">
            
                    
                    多客户端整合
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                    插件
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../plugins/logger.html">
            
                <a href="../plugins/logger.html">
            
                    
                    系统日志插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../plugins/reqlog.html">
            
                <a href="../plugins/reqlog.html">
            
                    
                    请求日志插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../plugins/metrics.html">
            
                <a href="../plugins/metrics.html">
            
                    
                    监控指标插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../plugins/tracing.html">
            
                <a href="../plugins/tracing.html">
            
                    
                    请求追踪插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="../plugins/profiling.html">
            
                <a href="../plugins/profiling.html">
            
                    
                    性能分析插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="../plugins/version.html">
            
                <a href="../plugins/version.html">
            
                    
                    版本信息插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="../plugins/healthcheck.html">
            
                <a href="../plugins/healthcheck.html">
            
                    
                    健康检查插件
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" >
            
                <span>
            
                    
                    框架开发者指南
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../topics/start.html">
            
                <a href="../topics/start.html">
            
                    
                    准备工作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../topics/log.html">
            
                <a href="../topics/log.html">
            
                    
                    log
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../topics/errors.html">
            
                <a href="../topics/errors.html">
            
                    
                    errors
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="../topics/definition.html">
            
                <a href="../topics/definition.html">
            
                    
                    definition
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="../topics/method.html">
            
                <a href="../topics/method.html">
            
                    
                    definition/method
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="../topics/converter.html">
            
                <a href="../topics/converter.html">
            
                    
                    operators/converter
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.7" data-path="../topics/validator.html">
            
                <a href="../topics/validator.html">
            
                    
                    operators/validator
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.8" data-path="../topics/router.html">
            
                <a href="../topics/router.html">
            
                    
                    service/router
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.9" data-path="../topics/service.html">
            
                <a href="../topics/service.html">
            
                    
                    service
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.10" data-path="../topics/nirvana.html">
            
                <a href="../topics/nirvana.html">
            
                    
                    nirvana
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.11" data-path="../topics/config.html">
            
                <a href="../topics/config.html">
            
                    
                    config
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.12" data-path="../topics/rest.html">
            
                <a href="../topics/rest.html">
            
                    
                    rest
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.13" data-path="../topics/utils.html">
            
                <a href="../topics/utils.html">
            
                    
                    utils
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.14" data-path="../topics/command.html">
            
                <a href="../topics/command.html">
            
                    
                    cmd/nirvana
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >错误包</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x9519;&#x8BEF;&#x5305;">&#x9519;&#x8BEF;&#x5305;</h1>
<p>&#x5305;&#x8DEF;&#x5F84;: <code>github.com/caicloud/nirvana/errors</code></p>
<h2 id="error-&#x4ECB;&#x7ECD;">Error &#x4ECB;&#x7ECD;</h2>
<p>&#x5728;&#x4E1A;&#x52A1;&#x51FD;&#x6570;&#x4E2D;&#xFF0C;&#x9664;&#x4E86;&#x6B63;&#x5E38;&#x7684;&#x8FD4;&#x56DE;&#x6570;&#x636E;&#x4EE5;&#x5916;&#xFF0C;&#x8FD8;&#x9700;&#x8981;&#x5904;&#x7406;&#x5404;&#x79CD;&#x9519;&#x8BEF;&#x3002;&#x5728; golang &#x4E2D;&#xFF0C;&#x9519;&#x8BEF;&#x9700;&#x8981;&#x5B9E;&#x73B0; error &#x63A5;&#x53E3;&#x3002;&#x5373;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">type</span> error <span class="hljs-keyword">interface</span> {
    Error() <span class="hljs-keyword">string</span>
}
</code></pre>
<p>&#x8FD9;&#x79CD;&#x5F62;&#x5F0F;&#x7684; error &#x63A5;&#x53E3;&#x53EA;&#x80FD;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x7279;&#x5B9A;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x800C;&#x4E0D;&#x80FD;&#x643A;&#x5E26;&#x66F4;&#x591A;&#x7684;&#x5173;&#x4E8E;&#x9519;&#x8BEF;&#x7684;&#x4FE1;&#x606F;&#x3002;&#x56E0;&#x6B64; Nirvana &#x5BF9;&#x4E8E;&#x8FD9;&#x79CD;&#x9519;&#x8BEF;&#xFF0C;&#x90FD;&#x4F1A;&#x4EE5; 500 Internal Server Error &#x7684;&#x5F62;&#x5F0F;&#x8FD4;&#x56DE;&#x7ED9;&#x5BA2;&#x6237;&#x7AEF;&#x3002;&#x4F46;&#x662F;&#x901A;&#x5E38;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x4E1A;&#x52A1;&#x4E3A;&#x4E86;&#x533A;&#x5206;&#x4E0D;&#x540C;&#x7684;&#x9519;&#x8BEF;&#xFF0C;&#x4F1A;&#x4F7F;&#x7528;&#x9519;&#x8BEF;&#x7801;&#x548C;&#x9519;&#x8BEF;&#x5B57;&#x7B26;&#x4E32;&#x6765;&#x5171;&#x540C;&#x6807;&#x8BC6;&#x4E00;&#x4E2A;&#x9519;&#x8BEF;&#xFF0C;&#x65B9;&#x4FBF;&#x5BA2;&#x6237;&#x7AEF;&#x8BC6;&#x522B;&#x3002;</p>
<p>&#x4E8E;&#x662F; Nirvana &#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x63A5;&#x53E3;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-comment">// Error is a common interface for error.</span>
<span class="hljs-comment">// If an error implements the interface, type handlers can</span>
<span class="hljs-comment">// use Code() to get a specified HTTP status code.</span>
<span class="hljs-keyword">type</span> Error <span class="hljs-keyword">interface</span> {
    <span class="hljs-comment">// Code is a HTTP status code.</span>
    Code() <span class="hljs-keyword">int</span>
    <span class="hljs-comment">// Message is an object which contains information of the error.</span>
    Message() <span class="hljs-keyword">interface</span>{}
}
</code></pre>
<p>&#x5728;&#x4E1A;&#x52A1;&#x51FD;&#x6570;&#x4E2D;&#xFF0C;&#x4ECD;&#x7136;&#x4EE5; error &#x7684;&#x5F62;&#x5F0F;&#x8FD4;&#x56DE;&#x9519;&#x8BEF;&#x3002;&#x4F46;&#x662F;&#x6846;&#x67B6;&#x4F1A;&#x68C0;&#x67E5;&#x8FD4;&#x56DE;&#x7684;&#x9519;&#x8BEF;&#x662F;&#x5426;&#x5B9E;&#x73B0;&#x4E86; Error &#x63A5;&#x53E3;&#x3002;&#x5982;&#x679C;&#x5B9E;&#x73B0;&#x4E86;&#xFF0C;&#x5C31;&#x4F1A;&#x4EE5; <code>Code()</code> &#x7684;&#x8FD4;&#x56DE;&#x503C;&#x4F5C;&#x4E3A; HTTP &#x72B6;&#x6001;&#x7801;&#xFF0C;<code>Message()</code> &#x7684;&#x8FD4;&#x56DE;&#x503C;&#x4F5C;&#x4E3A;&#x6570;&#x636E;&#x8FD4;&#x56DE;&#x3002;</p>
<h3 id="errors-&#x5305;">errors &#x5305;</h3>
<p>&#x4E3A;&#x4E86;&#x65B9;&#x4FBF;&#x4F7F;&#x7528;&#xFF0C;Nirvana &#x63D0;&#x4F9B;&#x4E86; <code>errors</code> &#x5305;&#x7528;&#x4E8E;&#x751F;&#x6210; error&#x3002;&#x521B;&#x5EFA; error &#x7684;&#x65B9;&#x5F0F;&#x6709;&#x4E24;&#x79CD;&#xFF1A;</p>
<ol>
<li>&#x65B9;&#x6CD5;&#x4E00;</li>
</ol>
<pre><code class="lang-go"><span class="hljs-keyword">var</span> somethingNotCorrect = errors.BadRequest.Build(<span class="hljs-string">&quot;ProjectName:ModuleName:SomethingNotCorrect&quot;</span>, <span class="hljs-string">&quot;${name} is not correct&quot;</span>)

<span class="hljs-keyword">func</span> SomeFunction() error {
    <span class="hljs-comment">// Do something</span>
    <span class="hljs-keyword">return</span> somethingNotCorrect.Error(something.Name)
}
</code></pre>
<ol>
<li>&#x65B9;&#x6CD5;&#x4E8C;</li>
</ol>
<pre><code class="lang-go"><span class="hljs-keyword">func</span> SomeFunction() error {
    <span class="hljs-keyword">return</span> errors.BadRequest.Error(<span class="hljs-string">&quot;${name} is not correct&quot;</span>, something.Name)
}
</code></pre>
<p>&#x8FD9;&#x4E24;&#x79CD;&#x65B9;&#x6CD5;&#x90FD;&#x53EF;&#x4EE5;&#x521B;&#x5EFA; error&#xFF0C;&#x4F46;&#x662F;&#x7B2C;&#x4E00;&#x79CD;&#x65B9;&#x6CD5;&#x6BD4;&#x7B2C;&#x4E8C;&#x79CD;&#x591A;&#x51FA;&#x66F4;&#x591A;&#x7279;&#x6027;&#xFF1A;</p>
<ul>
<li>&#x7B2C;&#x4E00;&#x79CD;&#x65B9;&#x6CD5;&#x652F;&#x6301;&#x4F7F;&#x7528; <code>somethingNotCorrect.Derived(err)</code> &#x7684;&#x5F62;&#x5F0F;&#x5224;&#x65AD;&#x4E00;&#x4E2A; err &#x662F;&#x5426;&#x7531;&#x8FD9;&#x4E2A;&#x9519;&#x8BEF;&#x5DE5;&#x5382;&#x751F;&#x6210;&#x3002;</li>
<li>&#x7B2C;&#x4E00;&#x79CD;&#x65B9;&#x6CD5;&#x5E26;&#x6709; Reason&#xFF0C;&#x65B9;&#x4FBF;&#x5BA2;&#x6237;&#x7AEF;&#x5224;&#x65AD;&#x9519;&#x8BEF;&#x7C7B;&#x578B;&#x3002;</li>
</ul>
<p>&#x56E0;&#x6B64;&#x6211;&#x4EEC;&#x5EFA;&#x8BAE;&#x59CB;&#x7EC8;&#x4F7F;&#x7528;&#x7B2C;&#x4E00;&#x79CD;&#x65B9;&#x6CD5;&#x8FDB;&#x884C;&#x9519;&#x8BEF;&#x7684;&#x521B;&#x5EFA;&#x3002;&#x53EA;&#x6709;&#x5728;&#x9519;&#x8BEF;&#x4EC5;&#x5728;&#x5185;&#x90E8;&#x4F7F;&#x7528;&#xFF0C;&#x800C;&#x4E14;&#x4E0D;&#x9700;&#x8981;&#x8FDB;&#x884C;&#x9519;&#x8BEF;&#x5224;&#x65AD;&#x7684;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x4E3A;&#x4E86;&#x7B80;&#x5316;&#x9519;&#x8BEF;&#x521B;&#x5EFA;&#x7684;&#x8FC7;&#x7A0B;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x7B2C;&#x4E8C;&#x79CD;&#x65B9;&#x6CD5;&#x3002;</p>
<h3 id="error-&#x7684;-reason">error &#x7684; Reason</h3>
<p>&#x5728;&#x5B9E;&#x9645;&#x7684;&#x4E1A;&#x52A1;&#x4E2D;&#xFF0C;HTTP &#x72B6;&#x6001;&#x7801;&#x5E76;&#x4E0D;&#x8DB3;&#x4EE5;&#x8868;&#x8FBE;&#x4E1A;&#x52A1;&#x4E2D;&#x7E41;&#x590D;&#x7684;&#x9519;&#x8BEF;&#x3002;&#x56E0;&#x6B64;&#x6211;&#x4EEC;&#x5C06; HTTP &#x72B6;&#x6001;&#x7801;&#x89C6;&#x4E3A;&#x9519;&#x8BEF;&#x5927;&#x7C7B;&#xFF08;&#x6BD4;&#x5982; NotFound &#x8868;&#x793A;&#x8D44;&#x6E90;&#x4E0D;&#x5B58;&#x5728;&#x6216;&#x8005;&#x627E;&#x4E0D;&#x5230;&#xFF09;&#xFF0C;&#x7136;&#x540E;&#x5728;&#x5927;&#x7C7B;&#x4E0B;&#x5B9A;&#x4E49;&#x4E1A;&#x52A1;&#x9700;&#x8981;&#x7684;&#x5177;&#x4F53;&#x9519;&#x8BEF;&#x3002;</p>
<p>&#x8FD9;&#x6837;&#x505A;&#x6709;&#x4E24;&#x4E2A;&#x4F18;&#x52BF;&#xFF1A;</p>
<ol>
<li>&#x901A;&#x8FC7; HTTP &#x72B6;&#x6001;&#x7801;&#x5373;&#x53EF;&#x5927;&#x81F4;&#x5224;&#x65AD;&#x4E00;&#x4E2A;&#x9519;&#x8BEF;&#x7684;&#x884C;&#x4E3A;</li>
<li>&#x901A;&#x8FC7;&#x5177;&#x4F53;&#x9519;&#x8BEF;&#x7684; Reason &#x6765;&#x552F;&#x4E00;&#x786E;&#x5B9A;&#x4E00;&#x4E2A;&#x9519;&#x8BEF;</li>
</ol>
<p>&#x5728;&#x5E38;&#x89C1;&#x7684;&#x5546;&#x4E1A; API &#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x4E5F;&#x7ECF;&#x5E38;&#x770B;&#x5230;&#x4F7F;&#x7528;&#x6570;&#x5B57; ID &#x6765;&#x6807;&#x5FD7;&#x7684;&#x9519;&#x8BEF;&#x3002;&#x4F46;&#x662F;&#x6570;&#x5B57; ID &#x7684;&#x8868;&#x8FBE;&#x6027;&#x5F88;&#x5DEE;&#xFF0C;&#x800C;&#x4E14;&#x5168;&#x5C40;&#x552F;&#x4E00;&#x6027;&#x6BD4;&#x8F83;&#x96BE;&#x7EF4;&#x62A4;&#x3002;&#x56E0;&#x6B64;&#x6211;&#x4EEC;&#x4F7F;&#x7528;&#x5B57;&#x7B26;&#x4E32;&#x4F5C;&#x4E3A;&#x9519;&#x8BEF;&#x7684; ID&#xFF0C;&#x4E5F;&#x5C31;&#x662F; Reason&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x5EFA;&#x8BAE; Reason &#x7684;&#x683C;&#x5F0F;&#x6EE1;&#x8DB3;&#xFF1A;</p>
<p><code>&#x9879;&#x76EE;&#x540D;[:&#x6A21;&#x5757;&#x540D;]:&#x9519;&#x8BEF;&#x540D;</code></p>
<h3 id="&#x56FD;&#x9645;&#x5316;">&#x56FD;&#x9645;&#x5316;</h3>
<p>&#x4F7F;&#x7528; errors &#x5305;&#x751F;&#x6210;&#x7684;&#x9519;&#x8BEF;&#x4F1A;&#x8BB0;&#x5F55;&#x6BCF;&#x4E2A;&#x5360;&#x4F4D;&#x7B26;&#x7684;&#x540D;&#x79F0;&#x548C;&#x503C;&#xFF0C;&#x4FDD;&#x5B58;&#x5728; data &#x5B57;&#x6BB5;&#x4E2D;&#xFF0C;&#x53EF;&#x4EE5;&#x5728;&#x5BA2;&#x6237;&#x7AEF;&#x4F7F;&#x7528; data &#x91CC;&#x7684;&#x503C;&#x8FDB;&#x884C;&#x6587;&#x672C;&#x56FD;&#x9645;&#x5316;&#x3002;</p>
<h2 id="&#x4F7F;&#x7528;&#x8303;&#x4F8B;">&#x4F7F;&#x7528;&#x8303;&#x4F8B;</h2>
<p>&#x5728;&#x4E1A;&#x52A1;&#x51FD;&#x6570;&#x4E2D;&#x4F7F;&#x7528; errors&#xFF1A;</p>
<pre><code class="lang-go">
<span class="hljs-keyword">var</span> messageNotExist = errors.NotFound.Build(<span class="hljs-string">&quot;MyProject:Message:MessageNotExist&quot;</span>, <span class="hljs-string">&quot;there is no message with id ${id}&quot;</span>)

<span class="hljs-comment">// GetMessage return a message by id.</span>
<span class="hljs-keyword">func</span> GetMessage(ctx context.Context, id <span class="hljs-keyword">int</span>) (*Message, error) {
    <span class="hljs-keyword">if</span> id &gt; <span class="hljs-number">100</span> {
        <span class="hljs-keyword">return</span> <span class="hljs-literal">nil</span>, messageNotExist.Error(id)
    }
    <span class="hljs-keyword">return</span> &amp;Message{
        ID:      id,
        Title:   <span class="hljs-string">&quot;This is an example&quot;</span>,
        Content: <span class="hljs-string">&quot;Example content&quot;</span>,
    }, <span class="hljs-literal">nil</span>
}
</code></pre>
<p>&#x7F16;&#x8BD1;&#x8FD0;&#x884C;&#x540E;&#x53EF;&#x4EE5;&#x5F97;&#x5230;&#x7ED3;&#x679C;&#xFF1A;</p>
<p>&#x8BBF;&#x95EE; <code>curl http://localhost:8080/apis/v1/messages/100</code>&#xFF0C;&#x5373;&#x53EF;&#x5F97;&#x5230;&#x4E00;&#x4E2A; 200 &#x7684;&#x54CD;&#x5E94;&#xFF0C;&#x54CD;&#x5E94;&#x4F53;&#x4E3A;&#xFF1A;</p>
<pre><code>{&quot;id&quot;:100,&quot;title&quot;:&quot;This is an example&quot;,&quot;content&quot;:&quot;Example content&quot;}
</code></pre><p>&#x8BBF;&#x95EE; <code>curl http://localhost:8080/apis/v1/messages/101</code>&#xFF0C;&#x5373;&#x53EF;&#x5F97;&#x5230;&#x4E00;&#x4E2A; 404 &#x7684;&#x54CD;&#x5E94;&#xFF0C;&#x54CD;&#x5E94;&#x4F53;&#x4E3A;&#xFF1A;</p>
<pre><code>{&quot;reason&quot;:&quot;MyProject:Message:MessageNotExist&quot;,&quot;message&quot;:&quot;there is no message with id 101&quot;,&quot;data&quot;:{&quot;id&quot;:&quot;101&quot;}}
</code></pre>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="destination.html" class="navigation navigation-prev " aria-label="Previous page: Destination Handler">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="method.html" class="navigation navigation-next " aria-label="Next page: 方法包">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"错误包","level":"1.3.10","depth":2,"next":{"title":"方法包","level":"1.3.11","depth":2,"path":"concepts/method.md","ref":"concepts/method.md","articles":[]},"previous":{"title":"Destination Handler","level":"1.3.9","depth":2,"path":"concepts/destination.md","ref":"concepts/destination.md","articles":[]},"dir":"ltr"},"config":{"plugins":[],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"language":"zh-hans","gitbook":"*"},"file":{"path":"concepts/error.md","mtime":"2019-08-06T09:48:58.000Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-10-20T10:33:16.586Z"},"basePath":"..","book":{"language":"zh-hans"}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

